package com.yanqu.road.dao.impl.activity.jiuzhou;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jiuzhou.JiuZhouActivityChampionData;
import com.yanqu.road.entity.activity.jiuzhou.JiuZhouChampionUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class JiuZhouChampionDataImpl extends TempDataObjectDao<JiuZhouActivityChampionData> {

    public static String getJiuZhouActivityChampionMapDataSql = "select * from `t_u_jiu_zhou_activity_champion_data`";
    public Map<Integer,JiuZhouActivityChampionData> getJiuZhouActivityChampionMapData() {
        Map<Integer,JiuZhouActivityChampionData> historyChampionMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getJiuZhouActivityChampionMapDataSql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                historyChampionMap.put(rs.getInt("activityId"),parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return historyChampionMap;
    }

    public static String getJiuZhouActivityChampionDataSql = "select * from `t_u_jiu_zhou_activity_champion_data` where `activityId`=?";
    public JiuZhouActivityChampionData getJiuZhouActivityChampionData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getJiuZhouActivityChampionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateJiuZhouActivityChampionDataSql = "update `t_u_jiu_zhou_activity_champion_data` set championMap=?,serverIdList=?,time=? where activityId=?";
    @Override
    protected boolean updateData(JiuZhouActivityChampionData jiuZhouActivityChampionData) {
        jiuZhouActivityChampionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jiuZhouActivityChampionData.getChampionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR,JSON.toJSONString(jiuZhouActivityChampionData.getServerIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouActivityChampionData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouActivityChampionData.getActivityId()));
        if (getDb().executeNoneQuery(updateJiuZhouActivityChampionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertJiuZhouActivityChampionDataSql = "insert into `t_u_jiu_zhou_activity_champion_data`(activityId,championMap,serverIdList,time) values (?,?,?,?)";
    @Override
    protected boolean addData(JiuZhouActivityChampionData jiuZhouActivityChampionData) {
        jiuZhouActivityChampionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouActivityChampionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jiuZhouActivityChampionData.getChampionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR,JSON.toJSONString(jiuZhouActivityChampionData.getServerIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouActivityChampionData.getTime()));
        if (getDb().executeNoneQuery(insertJiuZhouActivityChampionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected JiuZhouActivityChampionData parseData(ResultSet rs) throws SQLException {
        JiuZhouActivityChampionData jiuZhouActivityChampionData = new JiuZhouActivityChampionData();
        jiuZhouActivityChampionData.setActivityId(rs.getInt("activityId"));
        jiuZhouActivityChampionData.setChampionMap(JSON.parseObject(rs.getString("championMap"), new TypeReference<Map<Integer, JiuZhouChampionUserData>>() {}));
        jiuZhouActivityChampionData.setServerIdList(JSON.parseObject(rs.getString("serverIdList"),new TypeReference<List<Long>>(){}));
        jiuZhouActivityChampionData.setTime(rs.getLong("time"));

        return jiuZhouActivityChampionData;
    }

}
